//
//  CBCReadTools.m
//  CBCReaderSDK
//
//  Created by 李雷川 on 2018/1/28.
//  Copyright © 2018年 李雷川. All rights reserved.
//

#import "CBCReadTools.h"
#import "CBCReaderDef.h"
@implementation CBCReadTools

+ (NSBundle *)cbc_readerBundle
{
    NSBundle *refreshBundle = nil;
    if (refreshBundle == nil) {
        // 这里不使用mainBundle是为了适配pod 1.x和0.x
        refreshBundle = [NSBundle bundleWithPath:[[NSBundle bundleForClass:[CBCReadTools class]] pathForResource:@"CBCReader" ofType:@"bundle"]];
    }
    return refreshBundle;
}

//获取阅读下载文件的BaseURL
+(NSString *)readingDownloadBaseUrl{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    BOOL isDebug = [userDefaults boolForKey:kDebugMode];
    if(isDebug){
         return @"http://testres.cbchot.com/attachment/book";
    }
    else{
        return @"http://res.cbchot.com/attachment/book";
    }
}



/**获取图书所在的文件夹路径*/
+ (NSString *)getCurrentBookPath:(NSString *)bookID{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *cachesDir = [paths objectAtIndex:0];
    NSString *bookDirectory = [cachesDir stringByAppendingPathComponent:@"book"];
    NSString *desPath = [bookDirectory stringByAppendingPathComponent:bookID];
    if([[NSFileManager defaultManager]fileExistsAtPath:desPath] == NO){
        [[NSFileManager defaultManager]createDirectoryAtPath:desPath withIntermediateDirectories:YES attributes:nil error:nil];
    }
    return desPath;
}


/**获取图书所在的文件夹路径*/
+ (NSString *)getBookChapterJsonPath:(NSString *)bookID{
    
    NSString *bookPath = [CBCReadTools getCurrentBookPath:bookID];
    NSLog(@"bookPath is:%@",bookPath);
    return [bookPath stringByAppendingPathComponent:@"chapter.json"];
}


/**获取图书目录文件地址*/
+ (NSString *)getChapterJsonUrl:(NSString *)bookID{
    
    NSString* path = [NSString stringWithFormat:@"%@/%@/chapter.json",[CBCReadTools readingDownloadBaseUrl],bookID];
    return path;
}

/**获取图书章节的下载地址*/
+ (NSString *)getChapterFileUrl:(NSUInteger)chapterNum andBookID:(NSString *)bookID{
    NSString* path = [NSString stringWithFormat:@"%@/%@/%@_%lu.CBCZ",[CBCReadTools readingDownloadBaseUrl],bookID,bookID,(unsigned long)chapterNum];
    return path;
}

/**获取当前小说章节的路径*/
+ (NSString *)getCBCZPath:(NSString *)fileName andBookID:(NSString *)bookID{
    NSString *bookPath = [CBCReadTools getCurrentBookPath:bookID];
    return [bookPath stringByAppendingPathComponent:fileName];
}

/**是否下载完成当前图书的目录文件*/
+ (BOOL)isDonwloadedBookDirectoryJson:(NSString *)bookID{
    NSString *jsonPath = [CBCReadTools getBookChapterJsonPath:bookID];
    if([[NSFileManager defaultManager]fileExistsAtPath:jsonPath]){
        return YES;
    }
    return NO;
}

/**是否下载完成当前章节的CBC文件*/
+ (BOOL)isDonwloadedBookChapterCBCZ:(NSUInteger)chapterNum andBookID:(NSString *)bookID{
    NSString *bookPath = [CBCReadTools getCurrentBookPath:bookID];
    NSString *cbczPath = [bookPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@_%lu.CBCZ",bookID,(unsigned long)chapterNum]];
    if([[NSFileManager defaultManager]fileExistsAtPath:cbczPath]){
        return YES;
    }
    return NO;
}
+(void)debugMode{
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    [userDefault setValue:@(YES) forKey:kDebugMode];
}
@end
